<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="40%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="学号" prop="student_no">
        <el-input v-model="form.student_no" placeholder="请输入学号"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="form.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="学校" prop="school_id">
        <el-select 
          v-model="form.school_id" 
          placeholder="请选择学校" 
          style="width: 100%"
          filterable
        >
          <el-option
            v-for="item in schoolOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-input v-model="form.grade" placeholder="请输入年级"></el-input>
      </el-form-item>
      <el-form-item label="班级" prop="class_name">
        <el-input v-model="form.class_name" placeholder="请输入班级"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from '@/api'

export default {
  name: 'StudentAdd',
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "新增学生",
      isEdit: false,
      form: {
        id: undefined,
        name: '',
        student_no: '',
        gender: 1,
        school_id: '',
        grade: '',
        class_name: '',
        phone: '',
        status: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        student_no: [
          { required: true, message: '请输入学号', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        school_id: [
          { required: true, message: '请选择学校', trigger: 'change' }
        ],
        grade: [
          { required: true, message: '请输入年级', trigger: 'blur' }
        ],
        class_name: [
          { required: true, message: '请输入班级', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ]
      },
      schoolOptions: []
    }
  },
  created() {
    this.getSchools()
  },
  methods: {
    getSchools() {
      http.schools.list().then(res => {
        this.schoolOptions = res.data
      })
    },
    open(row) {
      this.dialogVisible = true;
      if (row) {
        this.isEdit = true;
        this.dialogTitle = "编辑学生";
        this.form = { ...row };
      } else {
        this.isEdit = false;
        this.dialogTitle = "新增学生";
        this.form = {
          id: undefined,
          name: '',
          student_no: '',
          gender: 1,
          school_id: '',
          grade: '',
          class_name: '',
          phone: '',
          status: 1
        };
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              await http.students.update(this.form.id, this.form);
              this.$message.success("更新成功");
            } else {
              await http.students.create(null, this.form);
              this.$message.success("新增成功");
            }
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {
            console.error(error);
            if (error.response && error.response.data && error.response.data.message) {
              this.$message.error(error.response.data.message);
            } else {
              this.$message.error("操作失败，请检查学生信息是否重复");
            }
          }
        }
      });
    }
  }
}
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
</style>
